package net.nac.tbot;

import java.awt.AWTException;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;
import javax.swing.JTextField;

import net.nac.tbot.eltetris.Board;
import net.nac.tbot.eltetris.Bot;
import net.nac.tbot.eltetris.Move;
import net.nac.tbot.eltetris.TetraminoFactory;

public class TPanel extends JPanel {

  public final Dimension d = new Dimension(10 * 18, 20 * 18);
  private final int lightBlack = new Color(47, 47, 47).getRGB();
  private final int lightestBlack = new Color(77, 77, 77).getRGB();
  private final int darkBlack = new Color(43, 43, 43).getRGB();
  private final int partida1 = new Color(56, 56, 56).getRGB();
  private final int partida2 = new Color(79, 79, 79).getRGB();
  private Robot robot;
  private Rectangle rect;
  private Bot bot = new Bot();
  private int newPieceRGB;
  private int hold = -1;
  private int current = -1;
  private boolean pressedShift = false;
  private int lastRowsRemoved = 0;
  private JTextField delayTextField;

  public TPanel() {
    try {
      robot = new Robot();
    } catch (AWTException e) {
      e.printStackTrace();
    }
  }

  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    BufferedImage image = null;
    int height = getHeight();
    int width = getWidth();

    if (rect != null) {
      image = robot.createScreenCapture(rect);
    }

    g2d.setColor(Color.red);
    g2d.setStroke(new BasicStroke(2F));

    int gridHeight = 18;
    int gridWidth = 18;

    for (int i = 1; i < 10; i++) {
      g2d.drawLine(i * gridWidth, 0, i * gridWidth, height);
    }

    for (int i = 1; i < 20; i++) {
      g2d.drawLine(0, i * gridHeight, width, i * gridHeight);
    }

    g2d.setStroke(new BasicStroke(1F));
    int blankcount = 0;

    int[] board = new int[19];

    for (int i = 1, x = 19; i < 21; i++, x--) {
      String rowStr = "";
      for (int j = 1; j < 11; j++) {
        if (image != null) {
          int px = j * gridWidth - 9;
          int py = i * gridHeight - 9;

          int rgb = image.getRGB(px, py);

          if (rgb == lightBlack || rgb == darkBlack
                  || rgb == lightestBlack) {
            g2d.setColor(Color.white);
            int rx = j * gridWidth - 13;
            int ry = i * gridHeight - 13;
            g2d.fillRect(rx, ry, 8, 8);
            blankcount++;
            rowStr += "0";
          } else {
            g2d.setColor(new Color(rgb));
            int rx = j * gridWidth - 13;
            int ry = i * gridHeight - 13;
            g2d.fillRect(rx, ry, 8, 8);
            rowStr += "1";
//
//            if (i == 1 && (state == TState.STARTED || state == TState.WAIT_FOR_PIECE)) {
//              state = TState.SENDING_PIECE;
//              newPieceRGB = rgb;
//            }


          }
        }

      }
      if (!rowStr.isEmpty()) {
        if (x != 19) {
          board[x] = Integer.parseInt(new StringBuffer(rowStr).reverse().toString(), 2);
        }
      }

    }

  }

  @Override
  public Dimension getPreferredSize() {
    return d;
  }
}
